// miniprogram/pages/index/admin/review_job/review_job.js
const db = wx.cloud.database()
const _=db.command;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    start_jobs:[],
    btn: true
  },

  cpnyNote: function(event){
    
    const job_note=event.detail.value
    const jindex=event.target.dataset.jindex;
    this.data.start_jobs[jindex].job_obj.job_note=job_note;
  },
  check: function(event){
    const that = this;
    let tem="";
    wx.requestSubscribeMessage({
      tmplIds: ['iM5myfGg-CJVqdMbdArIBeH1-mrLYlnjspTwLvFtaZo'], 
      success: res=>{
        tem = res['iM5myfGg-CJVqdMbdArIBeH1-mrLYlnjspTwLvFtaZo'];
        if(tem=="accept"){
            wx.showToast({
            title: '已开启',
          })
          console.log(event);
          if(event.target.dataset.t=="yes"){
             that.onYes(event);
          }else{
            that.onNo(event);
          }
          
         
        }else{
          wx.showToast({
            title: '已拒绝',
          })
        }
      },
      fail: res=>{
          wx.showToast({
            title: '已拒绝',
          })
      }
    })
  },
  onYes: function(event){
    this.setData({btn: true})
    //改变obj_type为0,从数组中移除该元素
    const that=this;
    const jindex=event.target.dataset.jindex;
     
    const job_note= this.data.start_jobs[jindex].job_obj.job_note;
    const _id=this.data.start_jobs[jindex]._id;
  
    
    db.collection("job").doc(_id).update({
      data: {
        'job_obj.job_note': job_note,
        'job_obj.job_type': 0 
       
      }
    }).then(res=>{
       wx.cloud.callFunction({
        name: 'notice',
        data: {
          target: that.data.start_jobs[jindex].job_obj.openid,
          title: '发布成功',
          content: that.data.start_jobs[jindex].job_obj.job_name,
          note: '发布成功'
        }
      })
      let start_jobs=that.data.start_jobs;
      start_jobs.splice(jindex,1);
       that.setData({
        start_jobs: start_jobs
       })
      
       getApp().tos("Yes pass the job!");
    })
    this.setData({btn:false})
  },
  onNo: function(event){
    this.setData({btn: true})
    //从数据库移除元素
    //从数组移除元素
    const that=this;
    const jindex=event.target.dataset.jindex;
    let start_jobs=that.data.start_jobs;
    const job_note= this.data.start_jobs[jindex].job_obj.job_note;
    db.collection("job").doc(that.data.start_jobs[jindex]._id).remove().then(res=>{
      wx.cloud.callFunction({
        name: 'notice',
        data: {
          target: that.data.start_jobs[jindex].job_obj.openid,
          title: '发布失败',
          content: that.data.start_jobs[jindex].job_obj.job_name,
          note: job_note
        }
      })
      start_jobs.splice(jindex,1);
       that.setData({
        start_jobs: start_jobs
       })
      
       getApp().tos("no pass job!");
    })
    this.setData({btn: false})
  },
  /** 
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    // db.collection("job").where({
    //   'job_obj.job_type': -1
    // }).get().then(res=>{
    //   that.setData({start_jobs: res.data})
      
    // })
   this.loadData();
  },
  loadData: function(){
     const that=this;
     db.collection('job')
    .aggregate()
    .match({'job_obj.job_type': -1})
    .sample({size: 20})
    .end().then(res=>{
      
      that.setData({start_jobs: res.list})
      
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({btn: false})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
    this.loadData()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
   
  }
})